﻿@{
    ViewBag.Title = "Games";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table id="marbleTable" class="stripe row-border order-column">
    <thead>
        <th>ID</th>
        <th>Name</th>
        <th>Description</th>
        <th>Game Profile</th>
        <th hidden>Game Profile ID</th>
        <th>Game Profile Normal Price</th>
        <th>Game Profile VIP Price</th>
        <th>Normal Price</th>
        <th>VIP Price</th>
        <th>Repeat Play Discount %</th>
        <th>Game Company Name</th>
        <th>Notes</th>
        <th>Last UpdatedDate</th>
        <th>Last UpdatedBy</th>
    </thead>
    @foreach (var item in ViewBag.games)
    {  
        <tr>
            <td>
                <lable class="fixedCol1">@(item.Id == 0 ? "" : item.Id) </lable>
            </td>
            <td>
                <input type="text" class="fixedCol2" value="@item.Name"/>
            </td>
            <td>
                <input type="text" value="@item.Description"/>
            </td>
            <td>
                <select name="gameProfiles" id="gameProfile" onchange="onProfileChange(this)">
                    @foreach (var dow in item.GameProfiles)
                    {
                        if (@item.GameProfile == @dow.Id)
                        {
                        <option value="@dow.Name" id="@dow.Id" selected>@dow.Name</option>
                        }
                        else
                        {
                        <option value="@dow.Name"id="@dow.Id">@dow.Name</option>
                        }
                    }
                </select>
            </td>

            <td hidden>
                <input type="text" value="" />
            </td>
            <td>
                <input type="text" value="@item.GPNormalPrice" disabled />
            </td>
            <td>
                <input type="text" value="@item.GPVipPrice" disabled />
            </td>
            <td>
                <input type="text" value="@item.NormalPrice"/>
            </td>
            <td>
                <input type="text" value="@item.VIPPrice"/>
            </td>
            <td>
                <input type="text" value="@item.RepeatPlayDiscountPercentage"/>
            </td>
            <td>
                <input type="text" value="@item.GameCompanyName"/>
            </td>
            <td>
                <input type="text" value="@item.Notes"/>
            </td>
            <td>
                <input type="text" value="@item.LastUpdatedDate"disabled/>
            </td>
            <td>
                <input type="text" value="@item.LastUpdatedBy"disabled/>
            </td>

        </tr>  
    }
</table>
<div class="action">
    <button class="btn btn-primary btn-md" role="button" onclick="history.back();" id="back">Back</button>
    <button class="btn btn-primary btn-md" role="button" onclick="SaveGames()" id="saveGame">Save</button>
    <button class="btn btn-primary btn-md" role="button" id="addRow">Add</button>
    <button class="btn btn-primary btn-md" role="button" onclick="location.href='/Marble/index'">Close</button>

</div>
<script>
    $(document).ready(function () {
        $('.dropdown-toggle').collapse({
            toggle: false
        });
    })
    var myJsVariable = @Html.Raw(Json.Encode(ViewBag.games[0].GameProfiles));
    var updatedArray = [];
    function UpdatedList(index) {
        if (!updatedArray.includes(index)) {
            updatedArray.push(index);
        }
    }
 
    function onProfileChange(element) {
        for (var i = 0; i < myJsVariable.length; i++) {
            if (element.value == myJsVariable[i].Name) {
                element.parentElement.parentElement.cells[4].children[0].value = myJsVariable[i].Id;
                element.parentElement.parentElement.cells[5].children[0].value = myJsVariable[i].NormalPrice;
                element.parentElement.parentElement.cells[6].children[0].value = myJsVariable[i].VIPPrice;
            }
        }
    }
    function SaveGames() {
        if (updatedArray.length > 0) {

            var items = [];
            var table = document.getElementById('marbleTable');
            var fixedcolumn1 = $('.DTFC_Cloned lable.fixedCol1');
            var fixedcolumn2 = $('.DTFC_Cloned input.fixedCol2');
            for (var r = 0, n = table.rows.length; r < n; r++) {
                if (updatedArray.includes(r)) {
                    var rowIndex = r + 1;
                    var selectedProfileIndex = table.rows[rowIndex].cells[3].children[0].options["selectedIndex"];

                    items.push({
                        Id: fixedcolumn1[r].innerHTML,
                        Name: fixedcolumn2[r].value,
                        Description: table.rows[rowIndex].cells[2].children[0].value,
                        GameProfile: table.rows[rowIndex].cells[3].children[0].options[selectedProfileIndex].id,
                        NormalPrice: table.rows[rowIndex].cells[7].children[0].value,
                        VIPPrice: table.rows[rowIndex].cells[8].children[0].value,
                        RepeatPlayDiscountPercentage: table.rows[rowIndex].cells[9].children[0].value,
                        GameCompanyName: table.rows[rowIndex].cells[10].children[0].value,
                        Notes: table.rows[rowIndex].cells[11].children[0].value,
                    });
                }
            }
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: '@Url.Action("UpdateGames", "Game")',
                data: JSON.stringify({ games: items }),
                dataType: "json",
                success: function (data) {
                    alert('Data Saved');
                    location.reload();
                },
                error: function (e) {
                    alert(e);
                }
            });
        }
    }
   
    $(document).ready(function () {
        var table = $('#marbleTable').DataTable({
            scrollY: "400px",
            scrollX: true,
            scrollCollapse: true,
            editable: true,
            stateSave: true,
            // paging: true,
            bPaginate: false,
            bInfo: false,
            bFilter: false,
            bSort: false,
            "createdRow": function (row, data, index) {
                $('td', row).addClass('tblRow');
            },
            rowCallback: function (row, data) {
                var element = $('td', row);
                element.on("change", function (e) {
                    UpdatedList($(this).parent().index());
                });
            },
            //order: [[ 0, "desc" ]],
            fixedColumns: {
                leftColumns: 2
            }

        });
        $('#marbleTable tbody').on('click', 'tr', function () {
            if ($(this).hasClass('selected')) {
                $(this).removeClass('selected');
            }
            else {
                table.$('tr.selected').removeClass('selected');
                $(this).addClass('selected');
            }
        });
        $('#addRow').on('click', function () {
            $("#saveGame").attr("disabled", false);
            var fixedcolumn1values = $('.DTFC_Cloned lable.fixedCol1');
            var fixedcolumn2values = $('.DTFC_Cloned input.fixedCol2');

            var gameProfiles = '';
              @foreach (var dow in ViewBag.games[0].GameProfiles)
              {
                 @: gameProfiles = gameProfiles + '<option id="@dow.Id">@dow.Name</option>';
              }
            gameProfiles = gameProfiles + '</select>';
            var radomId = Math.random();
            table.row.add([
                '<lable class="fixedCol1"></lable>',
                '<input class="fixedCol2" type="text" value="" />',
                '<input type="text" value="" />',
                '<select name="gameProfiles" id="gameProfiles" onchange="onProfileChange(this)">' + gameProfiles,
                '<input type="text" id="'+radomId+'" value=""/>',
                '<input type="text" value="" disabled/>',
                '<input type="text" value="" disabled/>',
                '<input type="text" value="" />',
                '<input type="text" value="" />',
                '<input type="text" value="" />',
                '<input type="text" value="" />',
                '<input type="text" value="" />',
                '<input type="text" value="" disabled/>',
                '<input type="text" value="" disabled/>',
            ]).draw(false);
            document.getElementById(radomId).parentNode.hidden = true;
            for (var i = 0, j = fixedcolumn1values.length; i < j; i++) {
                $('.DTFC_Cloned lable.fixedCol1')[i].innerHTML = fixedcolumn1values[i].innerHTML;
            }
            for (var i = 0, j = fixedcolumn2values.length; i < j; i++) {
                $('.DTFC_Cloned input.fixedCol2')[i].value = fixedcolumn2values[i].value;
            }
        });
        $(".tblRow").on("change", function (e) { UpdatedList($(this).parent().index()); });
    });
   
    function ConfigureGame() {

    }
</script>
